package com.dgut.controller;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotRoleException;
import cn.dev33.satoken.stp.StpUtil;
import com.dgut.pojo.Area;
import com.dgut.pojo.Role;
import com.dgut.pojo.User;
import com.dgut.service.AreaService;
import com.dgut.service.RoleService;
import com.dgut.service.UserService;
import com.dgut.util.PageRequestInfo;
import com.dgut.util.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
@CrossOrigin
@Api("ManageController")
@RestController
@RequestMapping("manage")
public class ManageController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private AreaService areaService;

    /**
     * 当天未打卡名单
     * @param pageRequestInfo
     * @return
     */
    @PostMapping("/checkRecord")
    @ApiOperation(value = "当天未打卡名单")
    public Object checkRecord(@ApiParam(value = "分页请求",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){
                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin", "assist-admin");
                PageRequest pageRequest = PageRequest.of(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
                PageResult pageResult = userService.findUserByNoRecord(pageRequest);
                resultMap.put("pageCheckRecord",pageResult);
                resultMap.put("checkRecordResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("checkRecordResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("checkRecordResult",false);
        }
        return resultMap;
    }


    /**
     * 最近3天体温偏高学生名单
     * @param pageRequestInfo
     * @return
     */
    @PostMapping("/checkOverTemperature")
    @ApiOperation(value = "最近3天体温异常名单")
    public Object checkOverTemperature(@ApiParam(value = "分页请求",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                PageRequest pageRequest = PageRequest.of(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
                PageResult pageResult = userService.findUserByOverTemperature(pageRequest);
                resultMap.put("pageCheckOverTemperature",pageResult);
                resultMap.put("checkOverTemperatureResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("checkOverTemperatureResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("checkOverTemperatureResult",false);
        }
        return resultMap;
    }

    /**
     * 最近3天接触异常名单
     * @param pageRequestInfo
     * @return
     */
    @PostMapping("/checkOverContact")
    @ApiOperation(value = "最近3天接触异常名单")
    public Object checkOverContact(@ApiParam(value = "分页请求",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                PageRequest pageRequest = PageRequest.of(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
                PageResult pageResult = userService.findUserByOverContact(pageRequest);
                resultMap.put("pageCheckOverContact",pageResult);
                resultMap.put("checkOverContactResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("checkOverContactResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("checkOverContactResult",false);
        }
        return resultMap;
    }

    /**
     * 最近3天行程异常名单
     * @param pageRequestInfo
     * @return
     */
    @PostMapping("/checkOverJourney")
    @ApiOperation(value = "最近3天行程异常名单")
    public Object checkOverJourney(@ApiParam(value = "分页请求",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                PageRequest pageRequest = PageRequest.of(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
                PageResult pageResult = userService.findUserByOverJourney(pageRequest);
                resultMap.put("pageCheckOverJourney",pageResult);
                resultMap.put("checkOverJourneyResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("checkOverJourneyResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("checkOverJourneyResult",false);
        }
        return resultMap;
    }


    /**
     * 所有学生名单
     * @param pageRequestInfo
     * @return
     */
    @PostMapping("/findAllUser")
    @ApiOperation(value = "所有学生名单")
    public Object findAllUser(@ApiParam(value = "分页请求",required = true) @RequestBody PageRequestInfo pageRequestInfo){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                PageRequest pageRequest = PageRequest.of(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
                PageResult pageResult = userService.findAll(pageRequest);
                resultMap.put("pageFindAllUser",pageResult);
                resultMap.put("findAllUserResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("findAllUserResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("findAllUserResult",false);
        }
        return resultMap;
    }

    /**
     * 添加多个用户
     * @param user
     * @return
     */
    @ApiOperation(value = "添加多个用户")
    @PostMapping("/addUsers")
    public Object addUsers(@ApiParam(value = "用户列表")@RequestBody List<User> user){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                userService.saveUsers(user);
                resultMap.put("addUsersResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("addUsersResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("addUsersResult",false);
        }
        return resultMap;
    }

    /**
     * 添加单个用户
     * @param user
     * @return
     */
    @ApiOperation(value = "添加单个用户")
    @PostMapping("/addUser")
    public Object addUser(@ApiParam(value = "用户")@RequestBody User user){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                userService.saveUser(user);
                resultMap.put("addUserResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("addUserResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("addUserResult",false);
        }
        return resultMap;
    }

    @ApiOperation(value = "添加学生助手")
    @PostMapping("/addAssist")
    public Object addAssist(@ApiParam(value = "用户") @RequestBody User user){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                try{
                    user = userService.findUserById(user.getId());
                    Role role = new Role();
                    role.setId(user.getId());
                    role.setRole("assist-admin");
                    roleService.saveRole(role);
                    resultMap.put("addAssistResult",true);
                }catch (IllegalArgumentException e){
                    resultMap.put("Error","添加失败，无此学生");
                    resultMap.put("addAssistResult",false);
                }
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("addAssistResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("addAssistResult",false);
        }
        return resultMap;
    }



    /**
     * 添加风险地区
     * @param area
     * @return
     */
    @ApiOperation(value = "添加风险地区")
    @PostMapping("/addArea")
    public Object addArea(@ApiParam(value = "地区")@RequestBody Area area){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                areaService.saveArea(area);
                resultMap.put("addAreaResult",true);
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("addAreaResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("addAreaResult",false);
        }
        return resultMap;
    }


    /**
     * 删除风险地区
     * @param area
     * @return
     */
    @ApiOperation(value = "删除风险地区")
    @PostMapping("/deleteArea")
    public Object deleteArea(@ApiParam(value = "地区")@RequestBody Area area){
        HashMap resultMap = new HashMap();
        try {
            if(StpUtil.isLogin()){

                /**
                 * 权限检查
                 */
                StpUtil.checkRoleOr("manage-admin");
                try {
                    area = areaService.findAreaByName(area.getName());
                    areaService.deleteArea(area);
                    resultMap.put("deleteAreaResult",true);
                }catch (NotRoleException e){
                    resultMap.put("Error","无此风险地区");
                    resultMap.put("deleteAreaResult",false);
                }
            }else{
                resultMap.put("Error","请先登录");
                resultMap.put("deleteAreaResult",false);
            }
        }catch (NotRoleException e){
            resultMap.put("Error","无权限");
            resultMap.put("deleteAreaResult",false);
        }
        return resultMap;
    }
    @ExceptionHandler(NotLoginException.class)
    public Object handlerNotLoginException(NotLoginException nle, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        /**
         * 打印堆栈，以供调试
         */
        HashMap map = new HashMap();
        nle.printStackTrace();

        // 判断场景值，定制化异常信息
        String message = "";
        if(nle.getType().equals(NotLoginException.NOT_TOKEN)) {
            message = "未提供token";
        }
        else if(nle.getType().equals(NotLoginException.INVALID_TOKEN)) {
            message = "token无效";
        }
        else if(nle.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
            message = "token已过期";
        }
        else if(nle.getType().equals(NotLoginException.BE_REPLACED)) {
            message = "token已被顶下线";
        }
        else if(nle.getType().equals(NotLoginException.KICK_OUT)) {
            message = "token已被踢下线";
        }
        else {
            message = "当前会话未登录";
        }
        map.put("Error",message);
        return map;
    }

}
